<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="主体名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入主体名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册手机号" prop="mobilePhone">
        <el-input
          v-model="queryParams.mobilePhone"
          placeholder="请输入注册手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册时间" prop="registerTime">
        <el-date-picker clearable
          v-model="queryParams.registerTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择注册时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="是否有营业执照" prop="businessFlag">
        <el-select v-model="queryParams.businessFlag" placeholder="是否有营业执照" clearable size="small">
          <el-option label="是" :value="1" />
          <el-option label="否" :value="0" />
        </el-select>
      </el-form-item>

      <el-form-item  label="省" prop="province">
        <el-select v-model='queryParams.province' filterable placeholder="请选择省" clearable   >
          <el-option v-for='(temp,index) in mixinsProvinceList' :value="temp.name" :key='index' :label="temp.name"
                     @click.native='mixinsProvinceChange(temp.code)'/>
        </el-select>
      </el-form-item>
      <el-form-item  label="市" prop="city">
        <el-select v-model='queryParams.city' filterable placeholder="请选择市(先选择省)" clearable   >
          <el-option v-for='(temp,index) in mixinsCityList' :value="temp.name" :key='index' :label="temp.name"
                     @click.native='mixinsCityChange(temp.code)'/>
        </el-select>
      </el-form-item>
      <el-form-item  label="区/县" prop="area">
        <el-select v-model='queryParams.area' filterable placeholder="请选择区/县(请先选择市)" clearable   >
          <el-option v-for='(temp,index) in mixinsAreaList' :value="temp.name" :key='index' :label="temp.name"  @click.native='mixinsAreaChange(temp.code)'  />
        </el-select>
      </el-form-item>
      <el-form-item  label="乡镇/街道" prop="country" >
        <el-select v-model='queryParams.country' filterable placeholder="请选择街道(请先选择区/县)" clearable   >
          <el-option v-for='(temp,index) in mixinsStreetList' :value="temp.name" :key='index' :label="temp.name"/>
        </el-select>
      </el-form-item>

      <el-form-item label="主体类型" prop="mainType">
        <el-select v-model="queryParams.mainType" placeholder="请选择主体类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.main_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            />
        </el-select>
      </el-form-item>

      <el-form-item label="联系电话" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法人姓名" prop="legalPersonName">
        <el-input
          v-model="queryParams.legalPersonName"
          placeholder="请输入法人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['nongjt:perfect:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
<!--        <el-button-->
<!--          size="mini"-->
<!--          type="success"-->
<!--          icon="el-icon-edit"-->
<!--          @click="handleExamine"-->
<!--          v-hasPermi="['nongjt:perfect:edit']">-->
<!--          审核</el-button>-->
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
          v-hasPermi="['nongjt:perfect:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"

          @click="handleDelete"
          v-hasPermi="['nongjt:perfect:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['nongjt:perfect:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="perfectList" @selection-change="handleSelectionChange" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="注册手机号" align="center" prop="mobilePhone" width="109"/>
      <el-table-column label="主体名称" align="center" prop="companyName" />
      <el-table-column label="法人姓名" align="center" prop="legalPersonName" />
      <el-table-column label="统一社会信用代码" align="center" prop="creditCode" width="128"/>
      <el-table-column label="证件号码" align="center" prop="idNumber" width="160" />
      <el-table-column label="注册资金(万元)" align="center" prop="registerMoney" width="110"/>
      <el-table-column label="企业成立时间" align="center" prop="foundTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.foundTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center" prop="registerTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="center" prop="phoneNumber" />

      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="区/县" align="center" prop="area" />
      <el-table-column label="乡镇/街道" align="center" prop="country" />

      <el-table-column label="详细地址" align="center" prop="detailAddress" />
      <el-table-column label="所属行业" align="center" prop="industryType" width="170">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.industry_type" :value="scope.row.industryType"/>
        </template>
      </el-table-column>
      <el-table-column label="主体类型" align="center" prop="mainType" width="170">
        <template slot-scope="scope">
        <dict-tag :options="dict.type.main_type" :value="scope.row.mainType"/>
        </template>
      </el-table-column>
      <el-table-column label="生产类型" align="center" prop="produceType" width="170">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.produce_type" :value="scope.row.produceType"/>
        </template>
      </el-table-column>
      <el-table-column label="营业执照" align="center" prop="businessLicenseUrl" >
        <template slot-scope="scope">
          <div v-if="scope.row.businessLicenseUrl">
            <el-popover
              placement="right"
              width="300"
              trigger="hover">
              <el-image :src="getView(scope.row.businessLicenseUrl)" class="imgStyle"/>
              <el-image slot="reference" :src="getView(scope.row.businessLicenseUrl)" style="max-height: 50px;max-width: 150px"/>
            </el-popover>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="认证资质" align="center" prop="certificationQualificationUrl">
        <template slot-scope="scope">
          <div v-if="scope.row.certificationQualificationUrl">
            <el-popover
              placement="right"
              width="300"
              trigger="hover">
              <el-image :src="getView(scope.row.certificationQualificationUrl)" class="imgStyle"/>
              <el-image slot="reference" :src="getView(scope.row.certificationQualificationUrl)" style="max-height: 50px;max-width: 150px"/>
            </el-popover>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="品牌logo" align="center" prop="logoUrl" >
        <template slot-scope="scope">
          <div v-if="scope.row.logoUrl">
            <el-popover
              placement="right"
              width="300"
              trigger="hover">
              <el-image :src="getView(scope.row.logoUrl)" class="imgStyle"/>
              <el-image slot="reference" :src="getView(scope.row.logoUrl)" style="max-height: 50px;max-width: 150px"/>
            </el-popover>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="照片墙" align="center" prop="photoWall" />

      <el-table-column label="审核状态" align="center" prop="examineStatus" width="170">
        <template  slot-scope="scope">
          <el-tag v-if="scope.row.examineStatus===0" type="warning">待审核</el-tag>
          <el-tag v-if="scope.row.examineStatus===1" type="danger">不通过</el-tag>
          <el-tag v-if="scope.row.examineStatus===2" type="success">通过</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="审核时间" align="center" prop="examineTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.examineTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主体身份" align="center" prop="subjectIdentity" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleExamine(scope.row)"
            v-hasPermi="['nongjt:perfect:edit']"
          >审核</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['nongjt:perfect:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['nongjt:perfect:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--审核⽤户完善信息对话框-->
    <el-dialog title="审核完善信息" :visible.sync="examineDialog" width="700px" append-to-body>
      <el-form ref="examineForm" :model="examineForm" :rules="examineRules" label-width="auto">
        <el-form-item label="是否通过" prop="examineStatus">
          <el-radio-group v-model="examineForm.examineStatus">
            <el-radio-button :label="2">通过</el-radio-button>
            <el-radio-button :label="1">不通过</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意⻅" prop="examineMsg" >
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            v-model="examineForm.examineMsg">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="examineSubmit">确 定</el-button>
        <el-button @click="examineDialog = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改用户完善信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="注册手机号" prop="mobilePhone" >
          <el-input v-model="form.mobilePhone" disabled />
        </el-form-item>
        <el-form-item label="注册时间" prop="registerTime" >
          <el-date-picker clearable
            v-model="form.registerTime"
            type="date"
            value-format="yyyy-MM-dd"
            disabled>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="主体名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入主体名称" />
        </el-form-item>
        <el-form-item label="法人姓名" prop="legalPersonName">
          <el-input v-model="form.legalPersonName" placeholder="请输入法人姓名" />
        </el-form-item>
        <el-form-item label="证件号码" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入证件号码" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入联系电话" />
        </el-form-item>

        <el-form-item  label="省" prop="province">
          <el-select v-model='queryParams.province' filterable placeholder="请选择省" clearable   >
            <el-option v-for='(temp,index) in mixinsProvinceList' :value="temp.name" :key='index' :label="temp.name"
                       @click.native='mixinsProvinceChange(temp.code)'/>
          </el-select>
        </el-form-item>
        <el-form-item  label="市" prop="city">
          <el-select v-model='queryParams.city' filterable placeholder="请选择市(先选择省)" clearable   >
            <el-option v-for='(temp,index) in mixinsCityList' :value="temp.name" :key='index' :label="temp.name"
                       @click.native='mixinsCityChange(temp.code)'/>
          </el-select>
        </el-form-item>
        <el-form-item  label="区/县" prop="area">
          <el-select v-model='queryParams.area' filterable placeholder="请选择区/县(请先选择市)" clearable   >
            <el-option v-for='(temp,index) in mixinsAreaList' :value="temp.name" :key='index' :label="temp.name"  @click.native='mixinsAreaChange(temp.code)'  />
          </el-select>
        </el-form-item>
        <el-form-item  label="乡镇/街道" prop="country" >
          <el-select v-model='queryParams.country' filterable placeholder="请选择街道(请先选择区/县)" clearable   >
            <el-option v-for='(temp,index) in mixinsStreetList' :value="temp.name" :key='index' :label="temp.name"/>
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="detailAddress">
          <el-input v-model="form.detailAddress" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="地址编码" prop="addressCode">
          <el-input v-model="form.addressCode" disabled />
        </el-form-item>

        <el-form-item label="生产类型" prop="produceType">
          <el-select v-model="queryParams.produceType" placeholder="请选择生产类型" clearable size="small">
            <el-option
              v-for="dict in dict.type.produce_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属行业" prop="industryType">
          <el-select v-model="queryParams.industryType" placeholder="请选择所属行业" clearable size="small">
            <el-option
              v-for="dict in dict.type.industry_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="主体类型" prop="mainType">
          <el-select v-model="queryParams.mainType" placeholder="请选择主体类型" clearable size="small">
            <el-option
              v-for="dict in dict.type.main_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="creditCode" label-width="120px">
          <el-input v-model="form.creditCode" placeholder="请输入统一社会信用代码" />
        </el-form-item>
        <el-form-item label="企业成立时间" prop="foundTime">
          <el-date-picker clearable
                          v-model="form.foundTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择企业成立时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="注册资金" prop="registerMoney">
          <el-input v-model="form.registerMoney" placeholder="请输入注册资金" />
        </el-form-item>

        <el-form-item label="是否有营业执照" prop="businessFlag">
          <el-radio-group v-model="form.businessFlag">
            <el-radio-button :label="1">是</el-radio-button>
            <el-radio-button :label="0">否</el-radio-button>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="营业执照" prop="businessLicenseUrl">
          <imageUpload v-model="form.businessLicenseUrl"/>
        </el-form-item>
        <el-form-item label="认证资质" prop="certificationQualificationUrl">
          <imageUpload v-model="form.certificationQualificationUrl"/>
        </el-form-item>
        <el-form-item label="品牌logo" prop="logoUrl">
          <imageUpload v-model="form.logoUrl"/>
        </el-form-item>
        <el-form-item label="照片墙" prop="photoWall">
          <imageUpload v-model="form.photoWall" />
        </el-form-item>

<!--        <el-form-item label="照片墙介绍" prop="photoMsg">-->
<!--          <el-input v-model="form.photoMsg" placeholder="请输入照片墙介绍" />-->
<!--        </el-form-item>-->
        <el-form-item label="照片墙介绍" prop="photoMsg" >
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            v-model="form.photoMsg">
          </el-input>
        </el-form-item>

<!--        <el-form-item label="审核状态" prop="examineStatus">-->
<!--          <el-radio-group v-model="form.examineStatus">-->
<!--            <el-radio-button :label="1">是</el-radio-button>-->
<!--            <el-radio-button :label="0">否</el-radio-button>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="审核意见" prop="examineMsg">-->
<!--          <el-input v-model="form.examineMsg" placeholder="请输入审核未通过原因" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPerfect, getPerfect, delPerfect, addPerfect, updatePerfect } from "@/api/nongjt/perfect";
import {AreaMixins} from "@/mixins/AreaMixins";

export default {
  name: "Perfect",
  mixins:[AreaMixins],
  dicts:[
    'main_type',
    'produce_type',
    'industry_type'
  ],

  data() {
    return {

      //审核相关
      examineDialog: false,
      examineForm: {
        id: undefined,
        examineStatus: undefined,
        examineMsg: undefined
      },
      examineRules:
        {examineStatus:[{ required: true, message: "请选择审核状态", trigger: "blur" }],
        },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户完善信息表格数据
      perfectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mobilePhone: null,
        registerTime: null,
        companyName: null,
        legalPersonName: null,
        businessFlag: null,
        province: null,
        city: null,
        area: null,
        country: null,
        mainType: null,
        phoneNumber: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mobilePhone: [
          { required: true, message: "注册手机号不能为空", trigger: "blur" }
        ],
        companyName: [
          { required: true, message: "主体名称不能为空", trigger: "blur" }
        ],
        province: [
          { required: true, message: "省不能为空", trigger: "change" }
        ],
        city: [
          { required: true, message: "市不能为空", trigger: "change" }
        ],
        area: [
          { required: true, message: "区/县不能为空", trigger: "change" }
        ],
        country: [
          { required: true, message: "乡镇/街道不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户完善信息列表 */
    getList() {
      this.loading = true;
      listPerfect(this.queryParams).then(response => {
        this.perfectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        mobilePhone: null,
        registerTime: null,
        companyName: null,
        legalPersonName: null,
        businessFlag: null,
        businessLicenseUrl: null,
        certificationQualificationUrl: null,
        province: null,
        city: null,
        area: null,
        country: null,
        detailAddress: null,
        addressCode: null,
        produceType: null,
        mainType: null,
        subjectIdentity: null,
        industryType: null,
        phoneNumber: null,
        creditCode: null,
        idNumber: null,
        registerMoney: null,
        logoUrl: null,
        photoWall: null,
        photoMsg: null,
        longitude: null,
        latitude: null,
        foundTime: null,
        examineStatus: null,
        examineMsg: null,
        examineTime: null,
        sptPassword: null,
        sptLoginName: null,
        sptAuth: null,
        isSync: null,
        certificateTitleFlag: null,
        signUrl: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户完善信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPerfect(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户完善信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePerfect(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPerfect(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户完善信息编号为"' + ids + '"的数据项？').then(function() {
        return delPerfect(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('nongjt/perfect/export', {
        ...this.queryParams
      }, `perfect_${new Date().getTime()}.xlsx`)
    },
    /**
     *
     获取图⽚预览地址
     *
     * @param url
     * @returns {*|string}
     */
    getView(url) {
      //如果是⽹络路径
      if (url && url.startsWith('http')) {
        return url
      } else {
        //本地图⽚
        if (url && url.length > 0 && url.indexOf('[') === -1) {
        return process.env.VUE_APP_BASE_API + '/' + url
        }
      }
    },
    /**
     *
     点击审核按钮
     *
     * @param row
     */
    handleExamine(row) {
      this.examineForm = {
        id : row.id,
        examineStatus: undefined,
        examineMsg: undefined
      }
      this.examineDialog = true
    },
    /**
     *
     审核提交
     */
    examineSubmit() {
      this.$refs["examineForm"].validate(valid => {
        if (valid) {
          updatePerfect(this.examineForm).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.examineDialog = false;
            this.getList();
          });
        }
      });
    },
  }
};
</script>
